const CopyWebpackPlugin = require("copy-webpack-plugin");
const path = require("path");
// 复制文件到指定目录
const patterns = [
  {
    from: path.resolve("src/plugins/manifest.json"),
    to: `${path.resolve("dist")}/manifest.json`
  },
  {
    from: path.resolve("src/assets"),
    to: path.resolve("dist/assets")
  },
  {
    from: path.resolve("src/content/content.js"),
    to: path.resolve("dist/js")
  },
  {
    from: path.resolve("src/assets/FileSaver.min.js"),
    to: path.resolve("dist/js")
  },
  {
    from: path.resolve("src/assets/xlsx.min.js"),
    to: path.resolve("dist/js")
  },
  {
    from: path.resolve("src/worker.js"),
    to: path.resolve("dist/js")
  }
];

// 复制插件
const plugins = [
  new CopyWebpackPlugin({
    patterns: patterns
  }),
];

if (process.env.NODE_ENV !== "production") {
  plugins.push(
    new CopyWebpackPlugin({
      patterns: [
        {
          from: path.resolve("src/utils/hot-reload.js"),
          to: path.resolve("dist")
        }
      ],
      
    })
  );
}


// 页面文件
const pages = {};
// 配置 popup.html 页面
const chromeName = ["popup"];

chromeName.forEach(name => {
  pages[name] = {
    entry: `src/${name}/main.js`,
    template: `src/${name}/index.html`,
    filename: `${name}.html`
  };
});

module.exports = {
  pages,
  productionSourceMap: false,
  // 配置 content.js background.js
  configureWebpack: {
    devtool: 'source-map',
    // devtool: 'eval-cheap-source-map',
    entry: {
      background: "./src/background/main.js"
    },
    output: {
      filename: "js/[name].js"
    },
    plugins,
  },
  // 配置 content.css
  css: {
    extract: {
      filename: "css/[name].css"
    }
  },

  chainWebpack: config => {
    config.plugin('define').tap(definitions => {
      definitions['__VUE_PROD_HYDRATION_MISMATCH_DETAILS__'] = JSON.stringify(false);
      return definitions;
    });
  },

}
